package design2;


import java.util.Random;

/**
 * 算式抽象类 - 设计二的核心抽象类
 */
public abstract class BinaryOperation {
    protected int leftOperand;
    protected int rightOperand;
    protected char operator;
    protected Random random;
    
    /**
     * 默认构造方法
     */
    public BinaryOperation() {
        this.random = new Random();
        this.leftOperand = 0;
        this.rightOperand = 0;
        this.operator = '+';
    }
    
    /**
     * 带参数构造方法
     */
    public BinaryOperation(int left, int right, char op) {
        this();
        this.leftOperand = left;
        this.rightOperand = right;
        this.operator = op;
    }
    
    // 访问器方法
    public int getLeftOperand() {
        return leftOperand;
    }
    
    public int getRightOperand() {
        return rightOperand;
    }
    
    public char getOperator() {
        return operator;
    }
    
    /**
     * 抽象方法 - 计算算式结果
     */
    public abstract int calculate();
    
    /**
     * 抽象方法 - 检查算式合法性
     */
    public abstract boolean checkingCalculation();
    
    /**
     * 生成算式
     */
    public void generateOperation(char op) {
        this.operator = op;
        if (op == '+') {
            generateAdditionOperation();
        } else if (op == '-') {
            generateSubstractOperation();
        }
    }
    
    /**
     * 生成加法算式
     */
    protected void generateAdditionOperation() {
        this.leftOperand = random.nextInt(100);
        this.rightOperand = random.nextInt(100);
    }
    
    /**
     * 生成减法算式
     */
    protected void generateSubstractOperation() {
        this.leftOperand = random.nextInt(100);
        this.rightOperand = random.nextInt(this.leftOperand + 1);
    }
    
    /**
     * 比较两个算式是否相同
     */
    public boolean equals(BinaryOperation other) {
        if (other == null) return false;
        return this.leftOperand == other.leftOperand &&
               this.rightOperand == other.rightOperand &&
               this.operator == other.operator;
    }
    
    /**
     * 返回算式的字符串表示
     */
    @Override
    public String toString() {
        return leftOperand + "" + operator + "" + rightOperand + "=";
    }
}